package com.yunfeisoft.business.service.impl;

import com.yunfeisoft.base.controller.AttachmentController;
import com.yunfeisoft.base.dao.inter.AttachmentDao;
import com.yunfeisoft.base.service.inter.AttachmentService;
import com.yunfeisoft.business.dao.inter.OutHospitalApprovalResultDao;
import com.yunfeisoft.business.dao.inter.OutHospitalDao;
import com.yunfeisoft.business.model.OutHospital;
import com.yunfeisoft.business.model.OutHospitalApprovalResult;
import com.yunfeisoft.business.service.inter.OutHospitalApprovalResultService;
import com.yunfeisoft.common.base.BaseServiceImpl;
import com.yunfeisoft.common.sql.record.TransientField;
import com.yunfeisoft.common.sql.separation.DataSourceChange;
import com.yunfeisoft.common.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

/**
 * ClassName: OutHospitalApprovalResultServiceImpl
 * Description: 老人出院审批记录表service实现
 * Author: Jackie liu
 * Date: 2020-05-13
 */
@Service("outHospitalApprovalResultService")
public class OutHospitalApprovalResultServiceImpl extends BaseServiceImpl<OutHospitalApprovalResult, String, OutHospitalApprovalResultDao> implements OutHospitalApprovalResultService {

    @Autowired
    private AttachmentDao attachmentDao;

    @Autowired
    private OutHospitalDao outHospitalDao;

    @Override
    @DataSourceChange(slave = true)
    public Page<OutHospitalApprovalResult> queryPage(Map<String, Object> params) {
        return getDao().queryPage(params);
    }

    @Override
    public List<OutHospitalApprovalResult> queryListByOutId(String outId) {
        return getDao().queryListByOutId(outId);
    }

    /**
     * 医康部审批
     * @param approvalResult
     * @param attachmentIds
     */
    @Transactional
    @Override
    public int doctorSave(OutHospitalApprovalResult approvalResult, String[] attachmentIds) {
        // 添加审批记录
        save(approvalResult);
        // 如果上传附件
        if (!ObjectUtils.isEmpty(attachmentIds)) {
            // 更新附件关联的出院记录id
            attachmentDao.updateRefId(attachmentIds, approvalResult.getId());
        }
        // 更新出院记录状态
        Integer status = approvalResult.getStatus();
        OutHospital hospital = new OutHospital();
        hospital.setId(approvalResult.getOutHospitalId());
        if (OutHospitalApprovalResult.ApprovalStatusEnum.ACCEPT.getValue() == status) {
            // 医康部审批通过
            hospital.setStatus(OutHospital.OutHospitalStatusEnum.DOCTOR_ACCEPT.getValue());
        } else {
            // 医康部审批拒绝
            hospital.setStatus(OutHospital.OutHospitalStatusEnum.DOCTOR_REJECT.getValue());
        }
        // 更新出院记录
        return outHospitalDao.update(hospital);
    }

    /**
     * 护理部审批
     * @param params
     * @param attachmentIds
     * @return
     */
    @Transactional
    @Override
    public int nurseSave(Map<String, Object> params, String[] attachmentIds) {
        // 添加审批记录
        String outHospitalId = (String) params.get("outHospitalId");
        String content = (String) params.get("content");
        Integer status = (Integer) params.get("status");
        // 创建审批记录
        OutHospitalApprovalResult approvalResult = new OutHospitalApprovalResult();
        approvalResult.setContent(content);
        approvalResult.setStatus(status);
        approvalResult.setOutHospitalId(outHospitalId);
        // 保存
        save(approvalResult);
        // 保存上传的附件
        if (ObjectUtils.isEmpty(attachmentIds)) {
            attachmentDao.updateRefId(attachmentIds, approvalResult.getId());
        }
        // 更新出院记录状态
        OutHospital hospital = new OutHospital();
        hospital.setId(outHospitalId);
        if (OutHospitalApprovalResult.ApprovalStatusEnum.ACCEPT.getValue() == status) {
            hospital.setStatus(OutHospital.OutHospitalStatusEnum.NURSE_ACCEPT.getValue());
        } else {
            hospital.setStatus(OutHospital.OutHospitalStatusEnum.NURSE_REJECT.getValue());
        }
        return outHospitalDao.update(hospital);
    }

    /**
     * 后勤部审批
     * @param approvalResult
     * @param attachmentIds
     * @return
     */
    @Transactional
    @Override
    public int logisticsSave(OutHospitalApprovalResult approvalResult, String[] attachmentIds) {
        // 添加审批记录
        save(approvalResult);
        // 如果上传附件
        if (!ObjectUtils.isEmpty(attachmentIds)) {
            // 更新附件关联的出院记录id
            attachmentDao.updateRefId(attachmentIds, approvalResult.getId());
        }
        // 更新出院记录状态
        Integer status = approvalResult.getStatus();
        OutHospital hospital = new OutHospital();
        hospital.setId(approvalResult.getOutHospitalId());
        if (OutHospitalApprovalResult.ApprovalStatusEnum.ACCEPT.getValue() == status) {
            // 后勤部审批通过
            hospital.setStatus(OutHospital.OutHospitalStatusEnum.LOGISTICS_ACCEPT.getValue());
        } else {
            // 后勤部审批拒绝
            hospital.setStatus(OutHospital.OutHospitalStatusEnum.LOGISTICS_REJECT.getValue());
        }
        // 更新出院记录
        return outHospitalDao.update(hospital);
    }

    /**
     * 院办审批
     * @param approvalResult
     * @param attachmentIds
     * @return
     */
    @Transactional
    @Override
    public int contractSave(OutHospitalApprovalResult approvalResult, String[] attachmentIds) {
        // 添加审批记录
        save(approvalResult);
        // 如果上传附件
        if (!ObjectUtils.isEmpty(attachmentIds)) {
            // 更新附件关联的出院记录id
            attachmentDao.updateRefId(attachmentIds, approvalResult.getId());
        }
        // 更新出院记录状态
        Integer status = approvalResult.getStatus();
        OutHospital hospital = new OutHospital();
        hospital.setId(approvalResult.getOutHospitalId());
        if (OutHospitalApprovalResult.ApprovalStatusEnum.ACCEPT.getValue() == status) {
            // 院办审批通过
            hospital.setStatus(OutHospital.OutHospitalStatusEnum.CONTRACT_ACCEPT.getValue());
        } else {
            // 院办审批拒绝
            hospital.setStatus(OutHospital.OutHospitalStatusEnum.CONTRACT_REJECT.getValue());
        }
        // 更新出院记录
        return outHospitalDao.update(hospital);
    }
}